import axios from 'axios'
import request from '@/utils/request'

const getToken = () => localStorage.getItem('token');

export function login(userName, password) {
    const data = {
      userName,
      password
    }
    return request({
      url: '/user/login',
      headers: {
        isToken: true,
        repeatSubmit: false,
        'Content-Type': 'application/json'
      },
      method: 'post',
      data: data
    }).then(response => {
      console.log('登录返回数据：', response)
      return response;
    })
  }
  // 注册方法
export function register(data) {
    return request({
      url: '/user/register',
      headers: {
        isToken: false
      },
      method: 'post',
      data: data
    })
  }

export function testLogin(userName, password) {
  return axios({
    url: 'http://localhost:8081/user/login',
    method: 'post',
    data: {
      userName,
      password
    },
    headers: {
      'Content-Type': 'application/json'
    }
  }).then(response => {
    console.log('直接请求测试：', response);
  }).catch(error => {
    console.log('直接请求错误：', error);
  });
}

// 获取用户信息方法
export function getUserInfo() {
  const token = getToken();
  const headers = {
      isToken: true,
      repeatSubmit: false
  };

  if (token) {
      headers['Authorization'] = `${token}`;
  }

  console.log('getUserInfo 请求头信息:', headers); // 打印请求头信息

  return request({
      url: '/user/userInfo',
      headers,
      method: 'get',
      withCredentials: true,
  }).then(response => {
      console.log('获取用户信息返回数据：', response);
      return response;
  }).catch(error => {
      console.log('获取用户信息出错：', error);
      throw error;
  });
 
};